package org.realogic.cfm;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.realogic.cfm.authenticate.KSessionUser;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

/**
 * 系统上下文环境
 * 
 * @author chenkq
 * 
 */
public final class KAppContext {

	private KAppContext() {

	}

	private static ApplicationContext ac = null;

	private static File CONFIG_HOME = null;

	public static File getCfgHome() {
		if ((CONFIG_HOME == null) || (!CONFIG_HOME.exists()) || (!CONFIG_HOME.isDirectory())) {
			throw new java.lang.IllegalStateException("CONFIG_HOME未设置....");
		}
		return CONFIG_HOME;
	}

	public static void setContextConfigLocation(File configHome) {
		CONFIG_HOME = configHome;
	}

	public static void setApplicationContext(ApplicationContext wac) {
		ac = wac;
	}

	public static ApplicationContext getSpringApplicationContext() {
		return ac;
	}

	/**
	 * 获取当前登录用户
	 * 
	 * @return IUser
	 */
	public static KSessionUser currentUser() {
		KSessionUser tmp = null;
		tmp = new KSessionUser("P1001", "陈开群", "D1005", "开发部", "chenkq");
		return tmp;
	}

	public static KSessionUser currentUser(HttpServletRequest theRequest) {
		HttpSession session = theRequest.getSession(false);
		KSessionUser usom = (KSessionUser) session.getAttribute("USOM");
		if (usom == null) {
			usom = new KSessionUser();
			session.setAttribute("USOM", usom);
		}
		return usom;
	}

	public static boolean hasPermission(String permType, HttpServletRequest theRequest) {
		HttpSession session = theRequest.getSession(false);
		KSessionUser usom = (KSessionUser) session.getAttribute("USOM");
		return usom.hasPermission(permType);
	}

	public static java.sql.Timestamp currentTimestamp() {
		return new java.sql.Timestamp(System.currentTimeMillis());
	}

	/**
	 * 获取Spring MVC WebApplicationContext
	 * 
	 * @return WebApplicationContext
	 */
	public static WebApplicationContext getApplicationContext() {
		return null;
	}

	/**
	 * 根据Bean Id获取Spring容器中的JavaBean
	 * 
	 * @param beanId
	 * @return JavaBean
	 */
	public static <T> T getBean(String beanId) {
		return null;
	}

	/**
	 * 补充业务实体创建者(created)和修改者updated信息
	 * 
	 * @param aEntity
	 */
	public static void attachCreatedInfo(Object aEntity) {
		try {
			KSessionUser sessionUser = currentUser();
			if (sessionUser != null) {
				java.sql.Timestamp curentTime = new java.sql.Timestamp(System.currentTimeMillis());
				BeanUtils.setProperty(aEntity, "orgID", sessionUser.getOrgID());
				BeanUtils.setProperty(aEntity, "createdBy", sessionUser.getName());
				BeanUtils.setProperty(aEntity, "createdID", sessionUser.getUserID());
				BeanUtils.setProperty(aEntity, "createdOn", curentTime);

				BeanUtils.setProperty(aEntity, "updatedBy", sessionUser.getName());
				BeanUtils.setProperty(aEntity, "updatedID", sessionUser.getUserID());
				BeanUtils.setProperty(aEntity, "updatedOn", curentTime);
			}
		} catch (Exception e) {

		}
	}

	/**
	 * 补充业务实体修改者updated信息
	 * 
	 * @param aEntity
	 */
	public static void attachUpdatedInfo(Object aEntity) {
		try {
			KSessionUser sessionUser = currentUser();
			if (sessionUser != null) {
				java.sql.Timestamp curentTime = new java.sql.Timestamp(System.currentTimeMillis());
				BeanUtils.setProperty(aEntity, "updatedBy", sessionUser.getName());
				BeanUtils.setProperty(aEntity, "updatedID", sessionUser.getUserID());
				BeanUtils.setProperty(aEntity, "updatedOn", curentTime);
			}
		} catch (Exception e) {

		}
	}

	/**
	 * 补充业务实体审批者approved信息
	 * 
	 * @param aEntity
	 */
	public static void attachApprovedInfo(Object aEntity) {
		try {
			KSessionUser sessionUser = currentUser();
			if (sessionUser != null) {
				java.sql.Timestamp curentTime = new java.sql.Timestamp(System.currentTimeMillis());
				BeanUtils.setProperty(aEntity, "approvedBy", sessionUser.getName());
				BeanUtils.setProperty(aEntity, "approvedID", sessionUser.getUserID());
				BeanUtils.setProperty(aEntity, "approvedOn", curentTime);
			}
		} catch (Exception e) {

		}
	}

}
